/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.ui.effects {
	import com.proyecto404.core.Check;
	import com.proyecto404.core.collections.Dictionary;
	import com.proyecto404.core.log.fireflash.Console;
	import com.proyecto404.core.util.Mixin;
	
	import com.greensock.TweenMax;
	import com.greensock.easing.Elastic;
	import com.greensock.easing.Quad;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class MouseMagnet extends Mixin {
		
		protected static const	DISTANCE	:	Number	=	40;
		protected static const	FRICTION	:	Number	=	0.2;
		
		public function MouseMagnet(prototype:*) {
			super(prototype);
		}
		
		public override function initialize(obj:*,params:Dictionary):void {
			Check.require(obj is MovieClip, "MouseMagnet Mixin can only be installed on MovieClips");
			
			var mc:MovieClip = MovieClip(obj);
			setVariable(obj, "_startX", mc.x);
			setVariable(obj, "_startY", mc.y);
			setVariable(obj, "_enabled", false);
			setVariable(obj, "_distance",(params && params.get("distance"))?params.get("distance"):DISTANCE);
			setVariable(obj, "_friction",(params && params.get("friction"))?params.get("friction"):FRICTION);
			mc.addEventListener(MouseEvent.ROLL_OVER, overHandler);
			mc.addEventListener(MouseEvent.ROLL_OUT, outHandler);
		}
		
		public function enableMagnet(enabled:Boolean):void {
			//setVariable(this, "_enabled", enabled);
		}
		
		private function overHandler(e:MouseEvent):void {
			var mc:MovieClip;
			mc = MovieClip(e.target);

			setVariable(mc, "_enabled",true);
			
			mc.addEventListener(Event.ENTER_FRAME,update);
		}
		
		private function outHandler(e:MouseEvent):void {
			var mc:MovieClip;
			mc = MovieClip(e.target);

			setVariable(mc, "_enabled",false);
			
			mc.removeEventListener(Event.ENTER_FRAME,update);
			TweenMax.to(mc, 0.2, {x:Number(getVariable(mc, "_startX")), y:Number(getVariable(mc, "_startY")), ease:Quad.easeOut});
		}
		
		private function update(e:Event):void {
			var xOffset:Number;
			var yOffset:Number;
			
			var mc:MovieClip = MovieClip(e.target);
			
			if(getVariable(mc, "_enabled")) {
				xOffset = mc.parent.mouseX - Number(getVariable(mc, "_startX"));
				if(xOffset > Number(getVariable(mc, "_distance"))) xOffset = Number(getVariable(mc, "_distance"));
				if(xOffset < -Number(getVariable(mc, "_distance"))) xOffset = -Number(getVariable(mc, "_distance"));
								
				yOffset = mc.parent.mouseY - Number(getVariable(mc, "_startY"));
				if(yOffset > Number(getVariable(mc, "_distance"))) yOffset = Number(getVariable(mc, "_distance"));
				if(yOffset < -Number(getVariable(mc, "_distance"))) yOffset = -Number(getVariable(mc, "_distance"));
				
				mc.x += (Number(getVariable(mc, "_startX")) + xOffset - mc.x) * Number(getVariable(mc, "_friction"));
				mc.y += (Number(getVariable(mc, "_startY")) + yOffset - mc.y) * Number(getVariable(mc, "_friction"));
			}
		}
	}
}